#!/bin/sh
###BEGIN INIT INFO 
# Provides:         OpenAiProxy.API
# Required-Start:    
# Required-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:   0 1 6
# Description:        OpenAiProxy.API
### END INIT INFO
user="root"
dir="/home/deploy/OpenAiProxy.API/"
cmd="dotnet OpenAiProxy.API.dll"
name=`basename $0`
pid_file="/var/run/$name.pid"
stdout_log="/tmp/$name.log"
stderr_log="/tmp/$name.err"

get_pid() {
    cat "$pid_file"
}

is_running() {
    [ -f "$pid_file" ] && ps `get_pid` > /dev/null 2>&1
}

case "$1" in
	start)
		if is_running; then
			echo "$name is Already started"
		else 
			cd "$dir"
			sudo -u "$user" $cmd >> "$stdout_log" 2>> "$stderr_log" &
			echo $! > "$pid_file"
			echo "=== $name started ==="
		fi
		
	;;
	stop)
		echo `get_pid`
		kill `get_pid`
	;;
    *)
        echo “auto start failed”
        exit 1
	;;
esac

exit 0
